package com.xczl.demo.test;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.xczl.demo.domain.Goods;
import com.xczl.demo.service.GoodsService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:root-context.xml" })
public class GoodsServiceTestCase {

	@Autowired
	private GoodsService goodsService;

	/**
	 * 添加
	 */
	@Test
	@Transactional
	public void add() {
		Goods goods = createGoods();
		goodsService.add(goods);
	}

	private Goods createGoods() {
		Goods goods = new Goods();
		goods.setName("牛仔裤女");
		goods.setCateId(2);
		goods.setGoodsId(155);
		goods.setPicUrl("");
		goods.setPrice(BigDecimal.valueOf(199));
		goods.setState(Goods.STATE_ON);
		return goods;
	}

	/**
	 * 更新
	 */
	@Test
	@Transactional
	public void update() {
		// 添加商品
		Goods goods = createGoods();
		goodsService.add(goods);
		// 商品下架
		goodsService.setState(goods.getGoodsId(), Goods.STATE_OFF);
		// 修改商品
		goods.setCateId(1);
		goods.setName("春季牛仔裤女装");
		goods.setPicUrl("abc");
		goods.setPrice(BigDecimal.valueOf(99));
		goods.setState(Goods.STATE_ON);
		goodsService.update(goods);
		// 检查
		Goods temp = goodsService.findById(goods.getGoodsId());
		Assert.assertNotNull(temp);
		Assert.assertTrue(goods.getCateId().equals(temp.getCateId()));
		Assert.assertTrue(goods.getPrice().compareTo(temp.getPrice()) == 0);
		Assert.assertTrue(StringUtils.equals(goods.getName(), temp.getName()));
		Assert.assertTrue(StringUtils.equals(goods.getPicUrl(),
				temp.getPicUrl()));

	}

	/**
	 * 删除
	 */
	@Test
	@Transactional
	public void delete() {
		Goods goods = createGoods();
		goodsService.add(goods);
		goodsService.delete(goods);
		Goods temp = goodsService.findById(goods.getGoodsId());
		Assert.assertNull(temp);
	}
}
